# Load Data ---------------------------------------------------------------
library(haven)
library(psych)
library(scales)

bov <- read_dta("../Data/study4_raw.dta")

# Attention ---------------------------------------------------------------
bov$grid_check <- ifelse(bov$populism_full_7 == 5, 1, 0)

# bov$atten_sum <- rowSums(bov[, c("grid_check", "open_views_pass", "opencheck_pass")], na.rm = T)
# prop.table(table(bov$atten_sum))

# Need for Chaos ----------------------------------------------------------
bov$nfc_teardown <- (bov$chaos1_1*-1+6)
bov$nfc_needchaos <- (bov$chaos1_2*-1+6)
bov$nfc_destroy <- (bov$chaos1_3*-1+6)
bov$nfc_upholdorder <- (bov$chaos1_4)
bov$nfc_workinside <- (bov$chaos1_5)
bov$nfc_protectinstits <- (bov$chaos1_6)
bov$nfc_greatthings <- (bov$chaos1_7)
bov$nfc_disastFun <- (bov$chaos2_1*-1+6)
bov$nfc_disastRebuild <- (bov$chaos2_2*-1+6)
bov$nfc_burnsociety <- (bov$chaos2_3*-1+6)
bov$nfc_burninstits <- (bov$chaos2_4*-1+6)
bov$nfc_clearrules <- (bov$chaos2_5)
bov$nfc_disastForeign <- (bov$chaos2_6) # nfc_disastfear; check numbering
bov$nfc_chaosupset <- (bov$chaos2_7)
bov$nfc_respectproduct <- (bov$chaos2_8)

v <- grep("nfc_", names(bov), value = T)
omega(bov[, v], plot = F)

bov$nfc_f <- (bov$nfc_teardown + bov$nfc_needchaos + bov$nfc_destroy + bov$nfc_disastFun + bov$nfc_disastRebuild + bov$nfc_burnsociety + bov$nfc_burninstits)/7
bov$nfc_r <- (bov$nfc_upholdorder + bov$nfc_workinside + bov$nfc_protectinstits + bov$nfc_greatthings + bov$nfc_clearrules + bov$nfc_chaosupset + bov$nfc_respectproduct)/7

# cor(bov$nfc_f, bov$nfc_r, use = "complete.obs")

v_pw <- c("nfc_teardown", "nfc_needchaos", "nfc_destroy", "nfc_disastFun", "nfc_disastRebuild", "nfc_burnsociety", "nfc_burninstits")
v_nw <- c("nfc_upholdorder", "nfc_workinside", "nfc_protectinstits", "nfc_greatthings", "nfc_clearrules", "nfc_chaosupset", "nfc_respectproduct")
# omega(bov[, v_pw], plot = FALSE)
# omega(bov[, v_nw], plot = FALSE)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Populism ----------------------------------------------------------------
bov$pop_fewints <- (bov$populism_full_1*-1+6)
bov$pop_crooked <- (bov$populism_full_2*-1+6)
bov$pop_nomethink <- (bov$populism_full_3*-1+6)
bov$pop_polsimprove <- bov$populism_full_4
bov$pop_yesmethink <- bov$populism_full_5
bov$pop_benefitall <- bov$populism_full_6

v <- grep("pop_", names(bov), value = T)
omega(bov[, v], plot = F)

bov$pop_f <- (bov$pop_fewints + bov$pop_crooked + bov$pop_nomethink)/3
bov$pop_r <- (bov$pop_polsimprove + bov$pop_yesmethink + bov$pop_benefitall)/3

v_pw <- c("pop_fewints", "pop_crooked", "pop_nomethink")
v_nw <- c("pop_polsimprove", "pop_yesmethink", "pop_benefitall")
# omega(bov[, v_pw], plot = FALSE, nfactors = 2)
# omega(bov[, v_nw], plot = FALSE, nfactors = 2)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Political Violence ------------------------------------------------------
bov$viol_threatpols <- (bov$violence_1*-1+6)
bov$viol_bricks <- (bov$violence_2*-1+6)
bov$viol_stopbadgovt <- (bov$violence_3*-1+6)
bov$viol_bullets <- (bov$violence_4*-1+6)
bov$viol_noviol <- bov$violence_5
bov$viol_nonviolprot <- bov$violence_6
bov$viol_violunaccept <- bov$violence_7
bov$viol_notit4tat <- bov$violence_8

v <- grep("viol_", names(bov), value = T)
omega(bov[, v], plot = F)

bov$viol_f <- (bov$viol_threatpols + bov$viol_bricks + bov$viol_stopbadgovt + bov$viol_bullets)/4
bov$viol_r <- (bov$viol_noviol + bov$viol_nonviolprot + bov$viol_violunaccept + bov$viol_notit4tat)/4

v_pw <- c("viol_threatpols", "viol_bricks", "viol_stopbadgovt", "viol_bullets")
v_nw <- c("viol_noviol", "viol_nonviolprot", "viol_violunaccept", "viol_notit4tat")
# omega(bov[, v_pw], plot = FALSE)
# omega(bov[, v_nw], plot = FALSE)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Conspiratorial Thinking -------------------------------------------------
bov$consp_plots <- (bov$conspiracy_1*-1+6)
bov$consp_fewppl <- (bov$conspiracy_2*-1+6)
bov$consp_dkrun <- (bov$conspiracy_3*-1+6)
bov$consp_wars <- (bov$conspiracy_4*-1+6)
bov$consp_schoolexps <- bov$conspiracy_5
bov$consp_democWill <- bov$conspiracy_6 # consp_democWill
bov$consp_complex <- bov$conspiracy_7
bov$consp_democ <- bov$conspiracy_8
bov$consp_US <- bov$conspiracy_9

v <- grep("consp_", names(bov), value = T)
omega(bov[, v], plot = F)

bov$consp_f <- (bov$consp_plots + bov$consp_fewppl + bov$consp_dkrun + bov$consp_wars)/4
# bov$consp_r <- (bov$consp_schoolexps + bov$consp_democWill + bov$consp_complex + consp_democ + bov$consp_US)/5
bov$consp_r <- (bov$consp_schoolexps + bov$consp_democWill + bov$consp_democ + bov$consp_US)/4

v_pw <- c("consp_plots", "consp_fewppl", "consp_dkrun", "consp_wars")
v_nw <- c("consp_schoolexps", "consp_democWill", "consp_democ", "consp_US")
# omega(bov[, v_pw], plot = FALSE)
# omega(bov[, v_nw], plot = FALSE)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Racial Resentment -------------------------------------------------------
bov$rr_specfavr <- (bov$raceresent_1*-1+6)
bov$rr_thard <- (bov$raceresent_2*-1+6)
bov$rr_dless <- bov$raceresent_3
bov$rr_pdisc <- bov$raceresent_4

v <- grep("rr_", names(bov), value = T)
omega(bov[, v], plot = F)

bov$rr_f <- (bov$rr_specfavr + bov$rr_thard)/2
bov$rr_r <- (bov$rr_dless + bov$rr_pdisc)/2


v_pw <- c("rr_specfavr", "rr_thard")
v_nw <- c("rr_dless", "rr_pdisc")
# omega(bov[, v_pw], plot = FALSE, nfactors = 1)
# omega(bov[, v_nw], plot = FALSE, nfactors = 1)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Hostile Sexism ----------------------------------------------------------
bov$hs_control <- (bov$hs_full_1*-1+6)
bov$hs_exaggerate <- (bov$hs_full_2*-1+6)
bov$hs_leash <- (bov$hs_full_3*-1+6)
bov$hs_reasonable <- bov$hs_full_4
bov$hs_feministpower <- bov$hs_full_5
bov$hs_fewwomen <- bov$hs_full_6

v <- grep("hs_", names(bov), value = T)
v <- grep("full_", v, value = T, invert = T)
omega(bov[, v], plot = F)

bov$hs_f <- (bov$hs_control + bov$hs_exaggerate + bov$hs_leash)/3
bov$hs_r <- (bov$hs_reasonable + bov$hs_feministpower + bov$hs_fewwomen)/3


v_pw <- c("hs_control", "hs_exaggerate", "hs_leash")
v_nw <- c("hs_reasonable", "hs_feministpower", "hs_fewwomen")
# omega(bov[, v_pw], plot = FALSE, nfactors = 2)
# omega(bov[, v_nw], plot = FALSE, nfactors = 2)

omega(bov[, c(v_pw, v_nw)], plot = F)

# Anti-Democratic ---------------------------------------------------------
bov$antidemoc_handout <- (bov$antidem_1*-1+6)
bov$antidemoc_force <- (bov$antidem_2*-1+6) # check numbers
bov$antidemoc_patriotic <- (bov$antidem_3*-1+6)
bov$antidemoc_bendrules <- (bov$antidem_4*-1+6)
bov$antidemoc_debate <- bov$antidem_5
bov$antidemoc_acceptelex <- bov$antidem_6
bov$antidemoc_roomtradits <- bov$antidem_7
bov$antidemoc_leaderlaws <- bov$antidem_8

v <- grep("antidemoc_", names(bov), value = T)
omega(bov[, v], plot = F)

v_pw <- c("antidemoc_handout", "antidemoc_force", "antidemoc_patriotic", "antidemoc_bendrules")
v_nw <- c("antidemoc_debate", "antidemoc_acceptelex", "antidemoc_roomtradits", "antidemoc_leaderlaws")
# omega(bov[, v_pw], plot = FALSE)
# omega(bov[, v_nw], plot = FALSE)

omega(bov[, c(v_pw, v_nw)], plot = F)